﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PcArduinoControl
{
	/// <summary>
	/// Keeps track of last "maxPackets" valid packets sent and received.
	/// </summary>
	class PacketLog
	{
		Log mLog;
		int mMaxPackets;
		Queue<Tuple<DateTime, Packet>> mPackets;

		public PacketLog(Log log, int maxPackets)
		{
			mLog = log;
			mMaxPackets = maxPackets;
			mPackets = new Queue<Tuple<DateTime, Packet>>();
		}

		public void LogPacket(Packet packet)
		{
			if (mPackets.Count >= mMaxPackets)
				mPackets.Dequeue();

			mPackets.Enqueue(new Tuple<DateTime, Packet>(DateTime.Now, packet));
		}

		public void WriteToMainLog()
		{
			/*lock (mPackets)
			{
				mLog.WriteLine("Packet Log {");

				foreach (Tuple<DateTime, Packet> packet in mPackets)
				{
					mLog.WriteLine
					(
						packet.Item1.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " +
						packet.Item2.ToString()
					);
				}

				mPackets.Clear();

				mLog.WriteLine("} End of a Packet Log");
			}*/
		}
	}
}
